package com.example.hookapp.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.hookapp.utils.ThreadUtils;

import java.text.SimpleDateFormat;

public class HookLogMessageFragment extends BaseFragment {
    TextView tvMessageLog;

    LogMessageReceiver logMessageReceiver;
    @Override
    protected int provideContentViewId() {
        return 0;
    }

    @Override
    protected View provideContentView(ViewGroup container) {
        LinearLayout linearLayout = ViewCreater.LinearLayoutBuilder.newInstance(container.getContext())
                .setWidthMatch()
                .setHeightMatch()
                .setOrientationVertical()
                .build();

        ScrollView scrollView = new ScrollView(container.getContext());
        scrollView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        tvMessageLog = ViewCreater.TextViewBuilder.newInstance(container.getContext())
                .setTextColor(0xffffffff)
                .setWidthMatch()
                .setTextSize(12)
                .setGravity(Gravity.TOP | Gravity.LEFT)
                .setPadding(0, ConvertUtils.dp2px(10), 0, ConvertUtils.dp2px(10))
                .build();
        tvMessageLog.setTextIsSelectable(true);
        tvMessageLog.setText("--------日志信息----------");
        scrollView.addView(tvMessageLog);

        linearLayout.addView(scrollView);
        return linearLayout;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        tvMessageLog.setText(logMsgSb.toString());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        logMessageReceiver = new LogMessageReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(HookLogMessageFragment.class.getName());
        activity.registerReceiver(logMessageReceiver, intentFilter);
    }

    class LogMessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("msg");
            receiveMsg(msg);
        }
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static long line = 1;

    /**
     * 接收日志
     */
    private void receiveMsg(String txt) {
        if (TextUtils.isEmpty(txt)) {
            return;
        }
        String time = sdf.format(System.currentTimeMillis());
        logMsgSb.insert(0, line + "：" + time + "：" + txt + "\n\n");
        line++;
        ThreadUtils.runOnUiThread(() -> {
            if (tvMessageLog != null) {
                tvMessageLog.setText(logMsgSb.toString());
            }
        });
    }

    public void clearLog() {
        logMsgSb.delete(0, logMsgSb.length());
        logMsgSb.append("日志清理完成...");
        tvMessageLog.setText(logMsgSb.toString());
        line = 1;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        activity.unregisterReceiver(logMessageReceiver);
    }

    private static StringBuffer logMsgSb = new StringBuffer();
}
